package com.yuanqunsoft.alimigration.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.yuanqunsoft.alimigration.bean.ApplicationBean;
import com.yuanqunsoft.alimigration.dao.IApplicationDao;
import com.yuanqunsoft.alimigration.dao.IModuleDao;
import com.yuanqunsoft.alimigration.dao.IReportDao;
import com.yuanqunsoft.alimigration.dao.support.Page;
import com.yuanqunsoft.alimigration.model.Application;
import com.yuanqunsoft.alimigration.model.Report;
import com.yuanqunsoft.alimigration.service.IApplicationService;
import com.yuanqunsoft.alimigration.service.IReportService;

public class ApplicationServiceImpl implements IApplicationService {
    private IApplicationDao applicationDao;
    private IModuleDao moduleDao;
    private IReportDao reportDao;

    public IReportDao getReportDao() {
        return reportDao;
    }

    public void setReportDao(IReportDao reportDao) {
        this.reportDao = reportDao;
    }

    public IApplicationDao getApplicationDao() {
        return applicationDao;
    }

    public void setApplicationDao(IApplicationDao applicationDao) {
        this.applicationDao = applicationDao;
    }

    public IModuleDao getModuleDao() {
        return moduleDao;
    }

    public void setModuleDao(IModuleDao moduleDao) {
        this.moduleDao = moduleDao;
    }

    @Override
    public List<ApplicationBean> findByName(String name) {
        return this.toApplicationBeanList(this.applicationDao.findByName(name));
    }

    @Override
    public boolean addApplication(Application instance) {
        try {
            this.applicationDao.save(instance);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean deleteApplication(Integer appId) {
        try {
            Application instance = this.applicationDao.findById(appId);
            this.applicationDao.delete(instance);
            this.moduleDao.deleteByApplicationId(appId);
            return true;
        } catch (Exception e) {
            return false;
        }

    }

    @Override
    public List<?> getAll() {
        return this.toApplicationBeanList(this.applicationDao.findAll());
    }

    public boolean renameApplication(String name, Integer appId) {
        if (this.applicationDao.isNameExisted(name)) {
            return false;
        } else {
            Application instance = this.applicationDao.findById(appId);
            instance.setAppName(name);
            this.applicationDao.save(instance);
            return true;
        }
    }

    @Override
    public List<ApplicationBean> Query(String filters, String orders, Object... values) {
        String hql = "from Application as model";

        if (filters != null && filters != "")
            hql += " where " + filters;

        if (orders != null && orders != "")
            hql += " order by " + orders;

        return this.toApplicationBeanList(applicationDao.hqlQuery(hql, values));
    }

    @Override
    public Page<ApplicationBean> pageQuery(int startNo, int pageSize, String filters, String orders,
            Object... values) {
        String hql = "from Application as model";

        if ((filters != null) && (!filters.isEmpty()))
            hql += " where " + filters;

        if ((orders != null) && (orders != ""))
            hql += " order by " + orders;

        return this.toApplicationBeanPage(applicationDao.pagedQueryByStartNo(hql, startNo, pageSize, values));
    }

    @Override
    public boolean isNameExisted(String name) {
        return applicationDao.isNameExisted(name);
    }

    @Override
    public ApplicationBean findById(Integer appId) {
        return this.toApplicationBean(this.applicationDao.findById(appId));
    }

    public List<ApplicationBean> toApplicationBeanList(List<Application> appList) {
        List<ApplicationBean> appBeanList = new ArrayList<ApplicationBean>();
        for (Application app : appList) {
            appBeanList.add(toApplicationBean(app));
        }
        return appBeanList;
    }

    public ApplicationBean toApplicationBean(Application application) {
        ApplicationBean appBean = new ApplicationBean();
        appBean.setAppCreateTime(application.getAppCreateTime());
        appBean.setAppHasReport(application.getAppHasReport());
        appBean.setAppName(application.getAppName());
        appBean.setAppId(application.getAppId());
        if (application.getAllUser() != null) {
            appBean.setUserId(application.getAllUser().getUserId());
            appBean.setUserName(application.getAllUser().getUserName());
        }
        return appBean;
    }

    public Page<ApplicationBean> toApplicationBeanPage(Page<Application> appPage) {
        Page<ApplicationBean> appBeanPage = new Page<ApplicationBean>();
        appBeanPage.setStart(appPage.getStart());
        appBeanPage.setTotalCount(appPage.getTotalCount());
        appBeanPage.setPageSize(appPage.getPageSize());
        appBeanPage.setData(this.toApplicationBeanList(appPage.getData()));
        return appBeanPage;
    }

    @Override
    public Application findByApplicationId(Integer appId) {
        // TODO Auto-generated method stub
        return applicationDao.findById(appId);
    }

}
